package org.aftputil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.ImageIcon;

/**
 * to deal with file information which returned by linux server
 * such as "-rw-rw-rw- 1 ftp admin 16380 May 11 05:13 hello.jpg" 
 * and     "drw-rw-rw- 1 ftp admin 0 Jul 06 03:19 mysql "
 * @author andy yang
 */
public class AftpFileItemWin extends AftpFileItem 
{
    String fileInfo;
    String dirIcon = "doc\\DIR.PNG";
    String fileIcon = "doc\\FILE.PNG";
	public AftpFileItemWin(String info)
	{
		this.fileInfo = info.trim();
		 phrase();
	}
	
	@Override
	void phrase() 
	{
	  try
	  {
		  //set name
		  String [] names = fileInfo.split("[0-9]{2}:[0-9]{2}");
		  this.setName(names[1].trim());
		  
		  String [] items = this.fileInfo.split("\\s+");
		  if(items.length < 9) return;
		  
		  String tmp = null;
		  tmp = items[0]; //type and permission 
		  
		  //set type
		  if(tmp.startsWith("-"))
		  {
			  this.setType(AftpFileItem.FILE);
			  this.setImage(new ImageIcon(this.fileIcon));
		  }
		  else if (tmp.startsWith("d")) 
		  {
			  this.setType(AftpFileItem.DIRECTORY);
			  this.setImage(new ImageIcon(this.dirIcon));
		  }
		  
		  //set permission
		  tmp = tmp.substring(1,tmp.length());
		  this.setPermission(tmp);
		  
		  //set size;
		  tmp = items[4];
		  this.setSize(Long.valueOf(tmp));

		  //set date
		  tmp = items[5]+" "+items[6]+" "+items[7];
		  SimpleDateFormat sdf = new SimpleDateFormat("MMM dd hh:mm",Locale.ENGLISH);
		  this.setLastModified(sdf.parse(tmp));
		  

		  
		  
	  }
	  catch(Exception e){e.printStackTrace();}
	}
	
	public static void main(String [] args)
	{
		System.out.println(new AftpFileItemWin("-rw-rw-rw- 1 ftp admin 16380 May 11 05:13 he  ll  o.jpg"));
		
		System.out.println(new AftpFileItemWin("drw-rw-rw- 1 ftp admin 16380 May 11 05:13 hed dllo.jpg"));
	}

}
